package com.globallogic.university.documents;

import java.util.ArrayList;
import java.util.List;

import com.globallogic.university.events.Event;
import com.globallogic.university.events.Event.DaysOfWeek;
import com.globallogic.university.people.StudentGroup;

public class Schedule {

	private List<Event> events = new ArrayList<Event>();
	private List<StudentGroup> schedGroups = new ArrayList<StudentGroup>();


	public void addEvent(Event event) {
		if (getEventByTimeSlot(event.getDayOfWeek(), event.getTimeSlot()).isEmpty())
			events.add(event);
	}
	
	public void addGroupToSchedule(StudentGroup group) {
		schedGroups.add(group);
	}
	
	public List<StudentGroup> getSchedGroups() {
		return schedGroups;
	}
	

	public List<Event> getEventsList() {
		return events;
	}
	
	public ArrayList<Event> getDailyEventsListForGroup(StudentGroup group, DaysOfWeek dayOfWeek) {
		ArrayList<Event> dailyEvents = new ArrayList<Event>();
		for (Event event : getEventsList()) {
			if (event.getGroup().equals(group) && (event.getDayOfWeek().equals(dayOfWeek)))
				dailyEvents.add(event);
		}
		return dailyEvents;		
	}
	
	public ArrayList<Event> getEventByTimeSlot(DaysOfWeek dayOfWeek, int timeSlot) {
		ArrayList<Event> result = new ArrayList<Event>();
		for (Event event : getEventsList()) {
			if (event.getDayOfWeek().equals(dayOfWeek) && event.getTimeSlot() == timeSlot)
				result.add(event);
		}
		return result;		
	}
	
	public boolean eventMissingPeerInSchedule(Event event) {
		for (Event otherEvent : getEventsList()) // going over all events
			if (event.getSubject().equals(otherEvent.getSubject()) && 	// if the Subject of events is the same 
				event.getGroup().equals(otherEvent.getGroup()) &&	
				!event.getClass().equals(otherEvent.getClass()))		// and class names (Lecture/Seminar) are different    
					return false;										// then the event has got its peer event in the schedule
		return true;
	}

	@Override
	public String toString() {
		String result = "";
		String warning = "";
		for (StudentGroup group : getSchedGroups()) {
			result += "SCHEDULE FOR GROUP "+group.getGroupId()+":\n";
			for (DaysOfWeek dayOfWeek : DaysOfWeek.values()) {
				result += dayOfWeek.name()+":\n";
				for (Event event : getDailyEventsListForGroup(group, dayOfWeek)) {
					if (eventMissingPeerInSchedule(event))
						warning = " !!MISSING PEER EVENT!!";
					result += event.toString()+warning+"\n";
				}
			}
		}
		return result;
	}

}
